import WeCropper from '../../../we-cropper/we-cropper.js'
const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight - 50

Page({
  data: {
    cropperOpt: {
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 400) / 2,
        y: (height - 400) / 2,
        width: 400,
        height: 400
      }
    },
    type: '', // 上个页面要的字段 
  },
  onLoad(option) {
    const { cropperOpt } = this.data
    if (option.src) {
      cropperOpt.src = option.src
      new WeCropper(cropperOpt)
        .on('ready', (ctx) => {
        })
        .on('beforeImageLoad', (ctx) => {
          wx.showToast({
            title: '上传中',
            icon: 'loading',
            duration: 20000
          })
        })
        .on('imageLoad', (ctx) => {
          wx.hideToast()
        })
        .on('beforeDraw', (ctx, instance) => {
        })
        .updateCanvas()
    };
    if ('type' in option) {
      this.setData({
        type: option.type
      })
    }
  },

  touchStart(e) {
    this.wecropper.touchStart(e)
  },
  touchMove(e) {
    this.wecropper.touchMove(e)
  },
  touchEnd(e) {
    this.wecropper.touchEnd(e)
  },
  getCropperImage() {
    this.wecropper.getCropperImage((imgSrc) => {
      if (imgSrc) {
        let pages = getCurrentPages();//所有页面
        let prevPage = pages[pages.length - 2];//上一页面（倒数第二个是上级页面）
        prevPage.setData({//直接给上移页面赋值
          [this.data.type]:imgSrc
        });

        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  uploadTap() {
    const self = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        //  获取裁剪图片资源后，给data添加src属性及其值  
        self.wecropper.pushOrign(src)
      }
    })
  },

})